﻿using Microsoft.Xna.Framework.Input;

namespace Phoenix.Xna.Framework.Input.Keyboards
{
    /// <summary>
    /// Extensions to the Microsoft.Xna.Framework.Input.Keys object.
    /// </summary>
    public static class KeysExtensions
    {
        /// <summary>
        /// Determines if the key is up.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if up, otherwise false.</returns>
        public static bool IsKeyUp(this Keys key)
        {
            return KeyboardStateManager.Instance.CurrentState.IsKeyUp(key);
        }

        /// <summary>
        /// Determines if the key was up.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if up, otherwise false.</returns>
        public static bool WasKeyUp(this Keys key)
        {
            return KeyboardStateManager.Instance.PreviousState.IsKeyUp(key);
        }

        /// <summary>
        /// Determines if the key is down.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if down, otherwise false.</returns>
        public static bool IsKeyDown(this Keys key)
        {
            return KeyboardStateManager.Instance.CurrentState.IsKeyDown(key);
        }

        /// <summary>
        /// Determines if the key was down.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if down, otherwise false.</returns>
        public static bool WasKeyDown(this Keys key)
        {
            return KeyboardStateManager.Instance.PreviousState.IsKeyDown(key);
        }

        /// <summary>
        /// Determines if the key was pressed.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if pressed, otherwise false.</returns>
        public static bool WasKeyPressed(this Keys key)
        {
            return (KeyboardStateManager.Instance.PreviousState.IsKeyUp(key) && KeyboardStateManager.Instance.CurrentState.IsKeyDown(key)) ? true : false;
        }

        /// <summary>
        /// Determines if the key was released.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if released, otherwise false.</returns>
        public static bool WasKeyReleased(this Keys key)
        {
            return (KeyboardStateManager.Instance.PreviousState.IsKeyDown(key) && KeyboardStateManager.Instance.CurrentState.IsKeyUp(key)) ? true : false;
        }

        /// <summary>
        /// Determines if the key was held.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if held, otherwise false.</returns>
        public static bool WasKeyHeld(this Keys key)
        {
            return (KeyboardStateManager.Instance.PreviousState.IsKeyDown(key) && KeyboardStateManager.Instance.CurrentState.IsKeyDown(key)) ? true : false;
        }

        /// <summary>
        /// Determines if the key is a alpha character key.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if the key is a alpha character, otherwise false.</returns>
        public static bool IsAlpha(this Keys key)
        {
            if (key == Keys.A ||
                key == Keys.B ||
                key == Keys.C ||
                key == Keys.D ||
                key == Keys.E ||
                key == Keys.F ||
                key == Keys.G ||
                key == Keys.H ||
                key == Keys.I ||
                key == Keys.J ||
                key == Keys.K ||
                key == Keys.L ||
                key == Keys.M ||
                key == Keys.N ||
                key == Keys.O ||
                key == Keys.P ||
                key == Keys.Q ||
                key == Keys.R ||
                key == Keys.S ||
                key == Keys.T ||
                key == Keys.U ||
                key == Keys.V ||
                key == Keys.W ||
                key == Keys.X ||
                key == Keys.Y ||
                key == Keys.Z)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Determines if the key is a numeric character key.
        /// </summary>
        /// <param name="key">The extended object.</param>
        /// <returns>True if the key is a numeric character, otherwise false.</returns>
        public static bool IsNumeric(this Keys key)
        {
            if (key == Keys.D0 ||
                key == Keys.D1 ||
                key == Keys.D2 ||
                key == Keys.D3 ||
                key == Keys.D4 ||
                key == Keys.D5 ||
                key == Keys.D6 ||
                key == Keys.D7 ||
                key == Keys.D8 ||
                key == Keys.D9)
            {
                return true;
            }

            return false;
        }
    }
}
